<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i>后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>新增SPU</el-breadcrumb-item>
      <el-breadcrumb-item>{{$router.currentRoute.name}}</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
      <el-form-item label="商品类别" prop="categoryId">
        <el-cascader
            style="width: 100%;"
            placeholder="请选择父级类别"
            v-model="ruleForm.categoryId"
            :options="options"></el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">下一步</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      options: [],
      // 表单
      ruleForm: {},
      //表单规则
      rules: {
        categoryId: [
          {required: true, message: '请选择商品类别', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    loadCategoryTree(){
      let url = "http://localhost:9480/categories/tree";

      let localJwt = localStorage.getItem('localJwt');
      this.axios.create({'headers': {'Authorization': localJwt}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if(jsonResult.state == 20000){
          this.options = jsonResult.data;
        }else{
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    // 加载本地存储的表单数据
    loadLocalRuleForm() {
      let localRuleFormString = localStorage.getItem('localRuleForm');
      let localRuleForm = JSON.parse(localRuleFormString);
      if(localRuleForm && localRuleForm.categoryId){
        this.ruleForm = localRuleForm;
      }
    },
    submitForm(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (Array.isArray(this.ruleForm.categoryId)) {
            this.ruleForm.categoryId = this.ruleForm.categoryId.pop();
          }
          let url = "http://localhost:9480/categories/" + this.ruleForm.categoryId;
          console.log('url = ' + url);

          let localJwt = localStorage.getItem('localJwt');
          this.axios.create({'headers': {'Authorization': localJwt}})
              .get(url).then((response) => {
            let jsonResult = response.data;
            if(jsonResult.state == 20000){
              let category = jsonResult.data;
              if (category.isParent == 1) {
                let message = '错误：选择的类别必须不包含子级类别！';
                this.$alert(message, '警告', {
                  confirmButtonText: '确定',
                  callback: action => {
                  }
                });
              }else{
                this.ruleForm.categoryName = category.name;
                let ruleForm = JSON.stringify(this.ruleForm);
                localStorage.setItem("localRuleForm", ruleForm);
                this.$router.push('add-new-2');
              }
            }else if(jsonResult.state == 40400){
              this.$alert(jsonResult.message, '错误', {
                confirmButtonText: '确定',
                callback: action => {
                  this.loadCategoryTree();
                }
              });
            }else{
              this.$alert(jsonResult.message, '错误', {
                confirmButtonText: '确定',
                callback: action => {
                }
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.loadCategoryTree();
    this.loadLocalRuleForm();
  }
}
</script>